package com.cafuc.myapplicationcat;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CheckBox extends AppCompatActivity implements View.OnClickListener {
    private Button button_CheckBox_checkBox, button_CheckBox_return;
    private TextView title_CheckBox_1;
    private CharSequence item[] = new CharSequence[]{"tEST_1", "tEST_2", "tEST_3"};
    private boolean[] setMultiChoiceItems = new boolean[]{false, false, false};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        initView();
    }

    private void initView() {
        title_CheckBox_1 = findViewById(R.id.title_CheckBox_1);
        title_CheckBox_1.setOnClickListener(this);
        button_CheckBox_checkBox = findViewById(R.id.button_CheckBox_checkBox);
        button_CheckBox_return = findViewById(R.id.button_CheckBox_return);
        button_CheckBox_return.setOnClickListener(this);
        button_CheckBox_checkBox.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case (R.id.button_CheckBox_checkBox):
                Toast.makeText(CheckBox.this, "CB Start", Toast.LENGTH_LONG).show();
                AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        .setTitle("choose")
                        .setIcon(R.mipmap.ic_launcher_cat)
                        .setMultiChoiceItems(item, setMultiChoiceItems, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                setMultiChoiceItems[i] = b;
                            }
                        })
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                StringBuffer stringBuffer = new StringBuffer();
                                for (int j = 0; j < setMultiChoiceItems.length; j++) {
                                    /**
                                     * 将setMultiChoiceItems中为真的选项添加到StringBuffer
                                     */
                                    if (setMultiChoiceItems[j]) {
                                        stringBuffer.append(item[j]).append(" ");
                                    }
                                }
                                if (stringBuffer != null) {
                                    Toast.makeText(CheckBox.this, "" + stringBuffer, Toast.LENGTH_LONG).show();
                                    title_CheckBox_1.setText(stringBuffer);

                                }
                                dialogInterface.dismiss();
                            }
                        }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                /**
                 * 记得将builder的参数传入dialog
                 * 然后show（）
                 */
                dialog = builder.create();
                dialog.show();
                break;
            case (R.id.button_CheckBox_return):
                startActivity(new Intent(CheckBox.this, MainActivity.class));
                break;
            case (R.id.title_CheckBox_1):
                title_CheckBox_1.setText("Init now");
                break;
        }
    }
}